package com.ktjiaoyu.controller;

import com.ktjiaoyu.entity.Flower;
import com.ktjiaoyu.entity.SysUser;
import com.ktjiaoyu.service.FlowerService;
import com.ktjiaoyu.utils.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/main/flower")
public class FlowerController extends BaseController{
    private static final Logger LOGGER= LoggerFactory.getLogger(FlowerController.class);
    @Autowired
    private FlowerService flowerService;

    @RequestMapping("/page")
    public String page(Model model, Flower flower,
                       @RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo,
                       @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize){
        try {
            //查询花卉的分页信息
            Page<Flower> page=flowerService.findPage(flower,pageNo,pageSize);
            model.addAttribute("pageInfo",page);
            model.addAttribute("condition",flower);
        }catch (Exception e){
            LOGGER.error("FlowerController中的page方法执行发生错误。",e);
            throw new RuntimeException(e);
        }
        return "flower/page";
    }
    @GetMapping("/save")
    public String toSave(){
        return "flower/save";
    }
    @PostMapping("/save")
    public String doSave(Flower flower){
        try {
            boolean ok=false;
            if (flower.getFlowerId()==null){//添加
                ok=flowerService.save(flower);
            }else{//修改
                ok=flowerService.update(flower);
            }
            if (ok){
                //跳转到列表;重定向到分页方法
                return redirect("/main/flower/page");
            }else {
                //跳回新增页面
                return "flower/save";
            }
        }catch (Exception e){
            LOGGER.error("FlowerController中的doSave方法执行发生错误。",e);
            throw new RuntimeException(e);
        }
    }
    @GetMapping("/view/{id}")
    public String view(@PathVariable("id") Integer id,Model model){
        try {
            Flower flower=flowerService.findById(id);
            model.addAttribute("flower",flower);
        }catch (Exception e){
            LOGGER.error("FlowerController中的view方法执行发生错误。",e);
            throw new RuntimeException(e);
        }
        return "flower/view";
    }
    @GetMapping("/edit")
    public String toEdit(@RequestParam("id") Integer flowerId,Model model){
        try {
            Flower flower=flowerService.findById(flowerId);
            model.addAttribute("flower",flower);
        }catch (Exception e){
            LOGGER.error("FlowerController中的toEdit方法执行发生错误。",e);
            throw new RuntimeException(e);
        }
        return "flower/save";
    }
    @DeleteMapping("/del/{id}")
    @ResponseBody
    public Map<String, Object> del(@PathVariable("id") Integer flowerId){
        Map<String, Object> map=new HashMap<>();
        try {
            boolean ok=flowerService.delete(flowerId);
            if (ok){
                map.put("code",200);
                map.put("msg","删除成功！");
            }else {
                map.put("code",500);
                map.put("msg","删除失败！");
            }
        }catch (Exception e){
            map.put("code",500);
            map.put("msg","删除失败!");
            LOGGER.error("FlowerController中的del方法执行发生错误。 ", e);
            throw new RuntimeException(e);
        }
        return map;
    }
    @ResponseBody
    @GetMapping("/flowerNameExist")
    public Object flowerNameExist(@RequestParam("flowerName") String flowerName) {
        Map<String, Object> map = new HashMap<>();
        try {
            // 角色编号不可为空
            if (StringUtils.isEmpty(flowerName)) {
                map.put("exist", 0);
            } else {
                Flower flower = flowerService.findByAccount(flowerName);
                if (null != flower) {
                    map.put("exist", 1);
                } else {
                    map.put("exist", 0);
                }
            }
        } catch (Exception e) {
            LOGGER.error("SysUserController中的flowerNameExists方法执行发生错误", e);
            map.put("error", "出现异常，请联系管理员处理！ ");
        }
        return map;
    }
}
